package View.JPanel;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JTextField;

import org.jdesktop.swingx.JXPanel;

import View.MainUI;
import View.CoolSwing.CoolButton;
import View.CoolSwing.MyTextField;
import View.JFrame.JRenameFrame;
import View.JTable.JAnimationTable;

public class JArrorwPanel extends JXPanel implements ActionListener, KeyListener{
//	private DirectionButton backwardButton = new DirectionButton("Backward",DirectionButton.Direction.LEFT);
// 	private DirectionButton forwardButton  = new DirectionButton("Forward",DirectionButton.Direction.RIGHT);
 	private JButton backwardButton = new CoolButton(1);
 	private JButton forwardButton = new CoolButton(3);
 	private JButton renameButton = new CoolButton(2);
 	private JTextField address = new MyTextField();
 	private JAnimationTable jAnime;
 	private JRenameFrame rename;
 	
	public JArrorwPanel() {
		setOpaque(false);
		backwardButton.setText(MainUI.s.back);
		backwardButton.setIcon(MainUI.s.backIcon);
		renameButton.setText(MainUI.s.rename);
		renameButton.setIcon(MainUI.s.renameIcon);
		forwardButton.setText(MainUI.s.forward);
		forwardButton.setIcon(MainUI.s.forwardIcon);
		address.setPreferredSize(new Dimension(300,25));
		address.addKeyListener(this);
		add(backwardButton);
		add(renameButton);
		add(forwardButton);
		add(address);
		renameButton.addActionListener(this);
		backwardButton.addActionListener(this);
		forwardButton.addActionListener(this);
	}

	/**
	 * setAddress in the JTextField;
	 * @param s
	 */
	public void setAddress(String s) {
		address.setText(s);
	}

	/**
	 * regist the jAnime;
	 * @param jAnime
	 */
	public void registJAnime(JAnimationTable jAnime) {
		this.jAnime = jAnime;
	}
	
 	public JAnimationTable getJAnime() {
		return jAnime;
	}
 	
	@Override
	public void actionPerformed(ActionEvent e) {
		//backward;
		if (e.getSource() == backwardButton) { 
			if (jAnime.getNow() > 0) {
				File f = jAnime.getHistory().get(jAnime.getNow() - 1);
				jAnime.setNow(jAnime.getNow() - 1);
				jAnime.setDirectionary(f);
				
//		    	for (File fl:jAnime.getHistory()) {
//					System.out.println(fl.getName());
//				}
//				System.out.println("");
			}
		}
		//forward;
		else if (e.getSource() == forwardButton) {
			if (jAnime.getNow() < jAnime.getHistory().toArray().length - 1) {
				File f = jAnime.getHistory().get(jAnime.getNow() + 1);
				jAnime.setNow(jAnime.getNow() + 1);
				jAnime.setDirectionary(f);
			}
		}
		//rename;
		else if (e.getSource() == renameButton) {
			if (jAnime.getSelectedRow() != -1) {
				if (rename == null) {
					rename = new JRenameFrame(jAnime.getIndex(), (File) jAnime.getSelectedFile());
					rename.registJAnimationtable(jAnime);
					rename.setVisible(true);
				}
				else {
					rename.setFile(jAnime.getIndex(), (File) jAnime.getSelectedFile());
					rename.setVisible(true);
				}
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER ) {
			File f = new File(address.getText());
			if (f.exists()) {
				jAnime.setDirectionary(f);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}
}
